//兼容性补丁
;(function(constructor) {
    if (constructor &&
        constructor.prototype &&
        constructor.prototype.firstElementChild == null) {
        Object.defineProperty(constructor.prototype, 'firstElementChild', {
            get: function() {
                var node, nodes = this.childNodes, i = 0;
                while (node = nodes[i++]) {
                    if (node.nodeType === 1) {
                        return node;
                    }
                }
                return null;
            }
        });
    }
})(window.Node || window.Element);
if (window.NodeList && !NodeList.prototype.forEach) {
    NodeList.prototype.forEach = function (callback, thisArg) {
        thisArg = thisArg || window;
        for (var i = 0; i < this.length; i++) {
            callback.call(thisArg, this[i], i, this);
        }
    };
}
let toastElement = null;
function ajax(param) {
	return new Promise((resolve, reject) => {
		var xhr = new XMLHttpRequest();
		xhr.onreadystatechange = () => {
			if (xhr.readyState == 4) {
				if (xhr.status >= 400) {
					reject(xhr);
				} else {
					resolve(xhr.responseText);
				}
			}
		};
		//如果只传入了URL
		if (typeof(param) === 'string') {
			param = {
				"url": param,
				"type": "GET"
			};
		}
		const url = param.url;
		//POST请求和PUT请求
		if (param.type === 'POST' || param.type === 'PUT') {
			xhr.open(param.type, url, true);
			let sendContentType = true;
			if (param.header) {
				for (const k in param.header) {
					if (k.toLowerCase() === 'content-type') {
						sendContentType = false;
					}
					xhr.setRequestHeader(k, param.header[k]);
				}
			}
			switch (typeof(param.data)) {
				case 'undefined':
					param.data = '';
					break;
				case 'object':
					if (param.data instanceof FormData) {
						break;
					}
					if (param.encode === 'json') {
						param.data = JSON.stringify(param.data);
					} else {
						let p = [];
						for (const k in param.data) {
							if (typeof(param.data[k]) === 'object' && param.data[k] instanceof Array) {
								param.data[k].forEach((e) => {
									p.push(k + '[]=' + encodeURIComponent(e));
								});
							} else {
								p.push(k + '=' + encodeURIComponent(param.data[k]));
							}
						}
						param.data = p.join('&');
					}
					if (sendContentType) {
						xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					}
					break;
				default:
					break;
			}
			xhr.send(param.data);
		} else {
			xhr.open(param.type || 'GET', url, true);
			if (param.header) {
				for (const k in param.header) {
					xhr.setRequestHeader(k, param.header[k]);
				}
			}
			xhr.send();
		}
	});
}
function showToast(message) {
	if (toastElement === null) {
		toastElement = document.createElement('div');
		toastElement.className = 'mdl-js-snackbar mdl-snackbar';
		toastElement.innerHTML = '<div class="mdl-snackbar__text"></div><button class="mdl-snackbar__action" type="button"></button>';
		componentHandler.upgradeElement(toastElement, 'MaterialSnackbar');
		document.body.appendChild(toastElement);
	}
	toastElement.MaterialSnackbar.showSnackbar({"message": message});
}
//get url params
function getParams() {
	var params = {};
	var urlParts = location.href.split("?", 2);
	if (urlParts.length == 1) {
		return params;
	}
	urlParts[1].split("&").forEach((keyValue) => {
		var splitKeyValue = keyValue.split("=", 2);
		params[decodeURIComponent(splitKeyValue[0])] = decodeURIComponent(splitKeyValue[1]);
	});
	return params;
}
function getTemplate(name) {
	return document.getElementById('template-' + name).content.firstElementChild.cloneNode(true);
}